﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using mshtml;
using System.Threading;

namespace WebCrawling.Data.Extracting
{
    public static class Utility
    {
        public const string LOGPATH = "log.txt";
        public const string SCRAPEDURLSPATH = "scrapedurls.txt";
        public static string ToMonth(int number)
        {
            if (number > 0 && number < 13)
            {
                if (number < 10)
                {
                    return "0" + number;
                }
                else
                {
                    return number.ToString();
                }
            }
            else
            {
                return "01";
            }
        }

        public static void Log(string URL, string msg)
        {           
            AppendTextToFile(LOGPATH, String.Format("Message:{0}      request URL:{1}     datetime:{2}", msg, URL, DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss")));
        }

        public static List<Uri> dd()
        {
            List<Uri> list = new List<Uri>();
            if (File.Exists(SCRAPEDURLSPATH))
            {
                StreamReader sr = File.OpenText(SCRAPEDURLSPATH);
                String input;
                while ((input = sr.ReadLine()) != null)
                {

                }
                sr.Close();
            }
            return list;
        }
        public static void AppendTextToFile(string path, string msg)
        {
            Monitor.Enter(LOGPATH);
            System.IO.StreamWriter write = null;
            try
            {
                write = File.AppendText(path);
                write.WriteLine(msg);
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
            finally
            {
                write.Flush();
                write.Close();
            }
            Monitor.Pulse(LOGPATH);
            Monitor.Exit(LOGPATH);
        }

        public static void SiteSpecialProcessing(string html, Uri outputLink, Dictionary<Uri, Output> outputs, SiteType siteType)
        {
            switch (siteType)
            {
                case SiteType.FatSecret:
                    List<string> serving = new List<string>();
                    foreach (string matchValue in html.GetMatchGroups(@"<h2 style=""text-transform:capitalize;margin:0"">\s+<img.+?>(.+?)</h2>(.+?)<hr/>"))
                    {
                        serving.Add(matchValue.TrimWhiteSpace().RemoveHtmlTags().ToTitleCase());
                    }
                    List<string> items = html.GetMatchGroups(@"<h2 style=""text-transform:capitalize;margin:0"">\s+<img.+?>(.+?)</h2>(.+?)<hr/>", 2);
                    Output output = null;
                    for (int i = 0; i < items.Count; i++)
                    {
                        if (items[i].Contains(outputLink.AbsolutePath))
                        {
                            output = new Output();
                            output.Url = outputLink;
                            output.Fields.Add("Serving", serving[i]);
                        }
                    }
                    if (output != null)
                    {
                        outputs.Add(outputLink, output);
                    }
                    break;

            }
        }
        public static string ContentSpecialProcessing(this string text, KeyValuePair<Uri, Rule> rule)
        {
            string key = "";
            string value = "";

            foreach (string keyValue in rule.Value.FilterChars.Split('|').ToList())
            {
                if (keyValue.Split('=').ToList().Count == 2)
                {
                    key = keyValue.Split('=')[0];
                    value = keyValue.Split('=')[1].TrimStart('"').TrimEnd('"');
                }
                text = Regex.Replace(text, key, value, RegexOptions.IgnoreCase);
            }
            return text.Replace("{UrlPrefix}", rule.Value.URLRule.UrlPrefix);
        }

        public static string GenerateSql(Dictionary<string, string> listOutputField, string tableName)
        {
            string sql = "";
            //string s = "insert into " + tableName + "(" + fields + ") select " + values + " from dual where not exists(select * from " + tableName + " where " + tableName + "." + title + "='" + output.Url.ToString().Replace("'", "''") + "');";

            return sql;
        }

        public static string InlineStyle(string html)
        {
            html = html.FilterScript();
            IHTMLDocument2 htmlDocument = (IHTMLDocument2)new mshtml.HTMLDocument();
            htmlDocument.write(html);

            string CascadingStyleSheet = string.Empty;

            foreach (IHTMLStyleSheet element in htmlDocument.styleSheets)
            {
                CascadingStyleSheet += element.cssText;
                element.cssText = "";
            }
            CSSParser parser = new CSSParser();
            parser.Read(CascadingStyleSheet);

            IHTMLElementCollection allElements = htmlDocument.all;
            foreach (IHTMLElement element in allElements)
            {
                string cssText = string.Empty;
                string classNames = element.className;
                if (classNames != null)
                {
                    //Multiple CSS Classes on a Single Element
                    foreach (string className in classNames.Split(' ').ToArray())
                    {
                        if (parser.Classes.Keys.Contains(className))
                        {
                            foreach (var c in parser.Classes[className])
                            {
                                cssText += c.Key + ": " + c.Value + ";";
                            }
                        }
                    }
                    element.style.cssText = cssText + (element.style.cssText == null ? "" : element.style.cssText);
                    element.className = "";
                }
            }
            return "<HTML>" + htmlDocument.body.parentElement.innerHTML + "</HTML>";
        }


        public static void WriteSql(String filename, Output output, string category)
        {
            string title = "";
            string content = "";
            foreach (KeyValuePair<string, string> field in output.Fields)
            {
                if (field.Key == "Title")
                {
                    title = field.Value.ToSqlString();
                }
                if (field.Key == "Content")
                {
                    content = field.Value.ToSqlString();
                }
            }
            if (title.IsNotNullOrEmpty() && content.IsNotNullOrEmpty())
            {

                string summary = content.RemoveHtmlTags().ToSqlString();
                if (summary.Length > 100)
                {
                    summary = summary.Substring(0, 100);
                }

                StringBuilder builder = new StringBuilder();

                builder.AppendLine("INSERT INTO pre_portal_article_title SET `title`='" + title + "',`shorttitle`='',`author`='',`from`='',`fromurl`='',`dateline`=UNIX_TIMESTAMP(NOW()),`url`='',`allowcomment`='1',`summary`='" + summary + "',`catid`='" + category + "',`tag`='0',`status`='0',`highlight`='|||',`showinnernav`='0',`uid`='1',`username`='牛途',`id`='0';");
                builder.AppendLine("insert into pre_portal_article_content set `id`=0,`content`='" + content + "',`pageorder`=1,`aid`=(SELECT max( aid ) FROM `pre_portal_article_title`),`dateline`=UNIX_TIMESTAMP(NOW());");

                AppendTextToFile(filename, builder.ToString());
            }
        }
    }
}
